package com.tencent.bugly.beta.ui;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import com.google.android.exoplayer2.C;
import com.tencent.bugly.beta.Beta;
import com.tencent.bugly.beta.download.BetaReceiver;
import com.tencent.bugly.beta.download.DownloadTask;
import com.tencent.bugly.proguard.an;
import com.tencent.bugly.proguard.y;
import java.util.Locale;

/* compiled from: BUGLY */
/* loaded from: classes.dex */
public class c {
    public static c a = new c();
    public DownloadTask b;
    public y d;
    public b e;
    private Notification g;
    private long j;
    private NotificationCompat.Builder k;
    private boolean i = true;
    private Context h = com.tencent.bugly.beta.global.e.E.s;
    public String c = this.h.getPackageName() + ".beta.DOWNLOAD_NOTIFY";
    private NotificationManager f = (NotificationManager) this.h.getSystemService("notification");

    private c() {
        this.h.registerReceiver(new BetaReceiver(), new IntentFilter(this.c));
        b();
    }

    private void b() {
        if (Build.VERSION.SDK_INT >= 26) {
            NotificationChannel notificationChannel = new NotificationChannel("001", "bugly_upgrade", 2);
            notificationChannel.setDescription("bugly upgrade");
            notificationChannel.enableVibration(false);
            notificationChannel.setVibrationPattern(new long[]{0});
            this.f.createNotificationChannel(notificationChannel);
        }
    }

    public void a(DownloadTask downloadTask) {
        this.b = downloadTask;
        this.j = this.b.getSavedLength();
        this.i = downloadTask.isNeededNotify();
        if (this.i && com.tencent.bugly.beta.global.e.E.R) {
            this.f.cancel(1000);
            Intent intent = new Intent(this.c);
            intent.putExtra("request", 1);
            if (this.k == null) {
                if (Build.VERSION.SDK_INT >= 26) {
                    try {
                        this.k = new NotificationCompat.Builder(this.h, "001");
                    } catch (Throwable unused) {
                        this.k = new NotificationCompat.Builder(this.h);
                    }
                } else {
                    this.k = new NotificationCompat.Builder(this.h);
                }
            }
            NotificationCompat.Builder builder = this.k;
            NotificationCompat.Builder contentTitle = builder.setTicker(Beta.strNotificationDownloading + com.tencent.bugly.beta.global.e.E.y).setContentTitle(com.tencent.bugly.beta.global.e.E.y);
            Locale locale = Locale.getDefault();
            Object[] objArr = new Object[2];
            objArr[0] = Beta.strNotificationDownloading;
            objArr[1] = Integer.valueOf((int) (this.b.getTotalLength() != 0 ? (this.b.getSavedLength() * 100) / this.b.getTotalLength() : 0L));
            contentTitle.setContentText(String.format(locale, "%s %d%%", objArr)).setContentIntent(PendingIntent.getBroadcast(this.h, 1, intent, C.ENCODING_PCM_MU_LAW)).setAutoCancel(false);
            if (com.tencent.bugly.beta.global.e.E.f > 0) {
                this.k.setSmallIcon(com.tencent.bugly.beta.global.e.E.f);
            } else if (com.tencent.bugly.beta.global.e.E.z != null && com.tencent.bugly.beta.global.e.E.z.applicationInfo != null) {
                this.k.setSmallIcon(com.tencent.bugly.beta.global.e.E.z.applicationInfo.icon);
            }
            try {
                if (com.tencent.bugly.beta.global.e.E.g > 0 && this.h.getResources().getDrawable(com.tencent.bugly.beta.global.e.E.g) != null) {
                    this.k.setLargeIcon(com.tencent.bugly.beta.global.a.a(this.h.getResources().getDrawable(com.tencent.bugly.beta.global.e.E.g)));
                }
            } catch (Resources.NotFoundException e) {
                an.c(c.class, "[initNotify] " + e.getMessage(), new Object[0]);
            }
            this.g = this.k.build();
            this.f.notify(1000, this.g);
        }
    }

    public void a() {
        if (this.i && this.b != null && com.tencent.bugly.beta.global.e.E.R) {
            if (this.b.getSavedLength() - this.j > 307200 || this.b.getStatus() == 1 || this.b.getStatus() == 5 || this.b.getStatus() == 3) {
                this.j = this.b.getSavedLength();
                if (this.b.getStatus() == 1) {
                    this.k.setAutoCancel(true).setContentText(Beta.strNotificationClickToInstall).setContentTitle(String.format("%s %s", com.tencent.bugly.beta.global.e.E.y, Beta.strNotificationDownloadSucc));
                } else if (this.b.getStatus() == 5) {
                    this.k.setAutoCancel(false).setContentText(Beta.strNotificationClickToRetry).setContentTitle(String.format("%s %s", com.tencent.bugly.beta.global.e.E.y, Beta.strNotificationDownloadError));
                } else {
                    if (this.b.getStatus() == 2) {
                        NotificationCompat.Builder contentTitle = this.k.setContentTitle(com.tencent.bugly.beta.global.e.E.y);
                        Locale locale = Locale.getDefault();
                        Object[] objArr = new Object[2];
                        objArr[0] = Beta.strNotificationDownloading;
                        objArr[1] = Integer.valueOf((int) (this.b.getTotalLength() != 0 ? (this.b.getSavedLength() * 100) / this.b.getTotalLength() : 0L));
                        contentTitle.setContentText(String.format(locale, "%s %d%%", objArr)).setAutoCancel(false);
                    } else if (this.b.getStatus() == 3) {
                        NotificationCompat.Builder contentTitle2 = this.k.setContentTitle(com.tencent.bugly.beta.global.e.E.y);
                        Locale locale2 = Locale.getDefault();
                        Object[] objArr2 = new Object[2];
                        objArr2[0] = Beta.strNotificationClickToContinue;
                        objArr2[1] = Integer.valueOf((int) (this.b.getTotalLength() != 0 ? (this.b.getSavedLength() * 100) / this.b.getTotalLength() : 0L));
                        contentTitle2.setContentText(String.format(locale2, "%s %d%%", objArr2)).setAutoCancel(false);
                    }
                }
                this.g = this.k.build();
                this.f.notify(1000, this.g);
            }
        }
    }

    public synchronized void a(y yVar, b bVar) {
        this.d = yVar;
        this.e = bVar;
        this.f.cancel(1001);
        Intent intent = new Intent(this.c);
        intent.putExtra("request", 2);
        if (this.k == null) {
            if (Build.VERSION.SDK_INT >= 26) {
                this.k = new NotificationCompat.Builder(this.h, "001");
            } else {
                this.k = new NotificationCompat.Builder(this.h);
            }
        }
        NotificationCompat.Builder builder = this.k;
        builder.setTicker(com.tencent.bugly.beta.global.e.E.y + Beta.strNotificationHaveNewVersion).setContentTitle(String.format("%s %s", com.tencent.bugly.beta.global.e.E.y, Beta.strNotificationHaveNewVersion)).setContentIntent(PendingIntent.getBroadcast(this.h, 2, intent, C.ENCODING_PCM_MU_LAW)).setAutoCancel(true).setContentText(String.format("%s.%s", yVar.e.d, Integer.valueOf(yVar.e.c)));
        if (com.tencent.bugly.beta.global.e.E.f > 0) {
            this.k.setSmallIcon(com.tencent.bugly.beta.global.e.E.f);
        } else if (com.tencent.bugly.beta.global.e.E.z != null && com.tencent.bugly.beta.global.e.E.z.applicationInfo != null) {
            this.k.setSmallIcon(com.tencent.bugly.beta.global.e.E.z.applicationInfo.icon);
        }
        if (com.tencent.bugly.beta.global.e.E.g > 0 && this.h.getResources().getDrawable(com.tencent.bugly.beta.global.e.E.g) != null) {
            this.k.setLargeIcon(com.tencent.bugly.beta.global.a.a(this.h.getResources().getDrawable(com.tencent.bugly.beta.global.e.E.g)));
        }
        this.g = this.k.build();
        this.f.notify(1001, this.g);
    }
}
